package tongcheng.com.tongcheng.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.ui.yemian.mapui.DaiLiQiangDanActivity;
import tongcheng.com.tongcheng.ui.yemian.mapui.GeRenFaDanActivity;
import tongcheng.com.tongcheng.ui.yemian.mapui.InstallQiangDanActivity;
import tongcheng.com.tongcheng.ui.yemian.mapui.OnePersonQiangDanActivity;
import tongcheng.com.tongcheng.utils.Setting;

import static tongcheng.com.tongcheng.ui.yemian.shenfen.activity.BaoZhenJinActivity.baozhenInstance;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	private String APP_ID = "wx7dec8b795b8c7bc3";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this,APP_ID);

        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
//		Log.e(TAG, "onPayFinish, errCode = " + resp.errCode);
//		Toast.makeText(WXPayEntryActivity.this, resp.errCode, Toast.LENGTH_SHORT).show();
//		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle("提示");
//			builder.setMessage("微信支付结果" +String.valueOf(resp.errCode));
//			builder.show();
//		}

		int errCode = resp.errCode;
		if(errCode == 0){
			Toast.makeText(getApplicationContext(), "支付成功", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(getApplicationContext(), "支付失败", Toast.LENGTH_SHORT).show();
		}
		if(baozhenInstance != null){
			baozhenInstance.finish();
		}
		switch (Setting.getShenfen()) {
			case 1://企业
				startActivity(new Intent(WXPayEntryActivity.this, GeRenFaDanActivity.class));
				break;
			case 2://个人发单
				startActivity(new Intent(WXPayEntryActivity.this, GeRenFaDanActivity.class));
				break;
			case 3://代理商
				startActivity(new Intent(WXPayEntryActivity.this, DaiLiQiangDanActivity.class));
				break;
			case 4://个人抢单
				startActivity(new Intent(WXPayEntryActivity.this, OnePersonQiangDanActivity.class));
				break;
			case 5://安装人员
				startActivity(new Intent(WXPayEntryActivity.this, InstallQiangDanActivity.class));
				break;
		}
		finish();
	}
}